<?php

namespace app\admin\service\setting;

use app\common\model\log\OperationLog;
use tmcore\base\BaseService;
use tmcore\exceptions\ValidateException;

/**
 * 操作日志服务类
 */
class LogService extends BaseService
{
    /**
     * 获取操作日志列表
     * @param $adminId
     * @return array
     */
    public static function getList($adminId): array
    {
        // 搜索条件
        $searchParam = [
            '%like%' => ['admin_name','url','ip'],
            '=' => ['type'],
            'datetime' => ['create_time'],
        ];

        $extra_params = [['status', '=', 1], ['module', '=', 'admin']];
        $admin_id = request()->param('admin_id');
        if($admin_id) $extra_params[] = ['admin_id', '=', $admin_id == 'admin'?$adminId:$admin_id];

        $where = self::getSearchData($searchParam, $extra_params);

        // 返回分页数据
        return OperationLog::page($where, self::getPageData(),'id,action,auth_menu,admin_name,admin_id,url,type,params,ip,create_time', ['id' => 'desc']);
    }

    /**
     * 查看日志详情
     * @param $params
     * @return array
     */
    public static function detail($params) : array
    {
        if(empty($params['id'])) throw new ValidateException('参数异常');
        $row = OperationLog::where(['id' => $params['id']])->append(['status_desc'])->hidden(['result','error','trace'])
            ->findOrEmpty();
        if ($row->isEmpty()) return [];
        return $row->toArray();
    }
}